{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Tce3stUlHN0L"
      },
      "source": [
        "##### Copyright 2018 The TensorFlow Authors.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "tuOe1ymfHZPu"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "s7Bo2MipUnXX"
      },
      "source": [
        "# Install TensorFlow for C"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Birwb-khUOIq"
      },
      "source": [
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://www.tensorflow.org/install/lang_c\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/install/lang_c.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://github.com/tensorflow/docs/blob/master/site/en/install/lang_c.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a href=\"https://storage.googleapis.com/tensorflow_docs/docs/site/en/install/lang_c.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
        "  </td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kFmEkitOFJSw"
      },
      "source": [
        "TensorFlow provides a C API that can be used to build\n",
        "[bindings for other languages](https://github.com/tensorflow/docs/tree/master/site/en/r1/guide/extend/bindings.md).\n",
        "The API is defined in\n",
        "<a href=\"https://github.com/tensorflow/tensorflow/blob/master/tensorflow/c/c_api.h\" class=\"external\"><code>c_api.h</code></a>\n",
        "and designed for simplicity and uniformity rather than convenience.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Vk--31hqIwSV"
      },
      "source": [
        "## Nightly libtensorflow C packages\n",
        "\n",
        "libtensorflow packages are built nightly and uploaded to GCS for all supported\n",
        "platforms. They are uploaded to the\n",
        "[libtensorflow-nightly GCS bucket](https://storage.googleapis.com/libtensorflow-nightly)\n",
        "and are indexed by operating system and date built. For MacOS and Linux shared\n",
        "objects, there is a\n",
        "[script](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/ci_build/builds/libtensorflow_nightly_symlink.sh)\n",
        "that renames the `.so` files versioned to the current date copied into the\n",
        "directory with the artifacts."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qowtdsijFMYZ"
      },
      "source": [
        "## Supported Platforms\n",
        "\n",
        "TensorFlow for C is supported on the following systems:\n",
        "\n",
        "* Linux, 64-bit, x86\n",
        "* macOS, Version 10.12.6 (Sierra) or higher\n",
        "* Windows, 64-bit x86"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hnhAk8y-FSBN"
      },
      "source": [
        "## Setup"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "y50y01XUFVb2"
      },
      "source": [
        "### Download and extract\n",
        "\n",
        "<table>\n",
        "  <tr><th>TensorFlow C library</th><th>URL</th></tr>\n",
        "  <tr class=\"alt\"><td colspan=\"2\">Linux</td></tr>\n",
        "  <tr>\n",
        "    <td>Linux CPU only</td>\n",
        "    <td class=\"devsite-click-to-copy\"><a href=\"https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-2.13.0.tar.gz\">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-2.13.0.tar.gz</a></td>\n",
        "  </tr>\n",
        "  <tr>\n",
        "    <td>Linux GPU support</td>\n",
        "    <td class=\"devsite-click-to-copy\"><a href=\"https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-2.13.0.tar.gz\">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-2.13.0.tar.gz</a></td>\n",
        "  </tr>\n",
        "  <tr class=\"alt\"><td colspan=\"2\">macOS</td></tr>\n",
        "  <tr>\n",
        "    <td>macOS CPU only</td>\n",
        "    <td class=\"devsite-click-to-copy\"><a href=\"https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-darwin-x86_64-2.13.0.tar.gz\">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-darwin-x86_64-2.13.0.tar.gz</a></td>\n",
        "  </tr>\n",
        "  <tr class=\"alt\"><td colspan=\"2\">Windows\n",
        "    <aside class=\"caution\">\n",
        "      <p><b>Caution:</b> TensorFlow 2.10 was the last TensorFlow release that supported GPU on native-Windows.</p>\n",
        "    </aside>\n",
        "  </td></tr>\n",
        "  <tr>\n",
        "    <td>Windows CPU only</td>\n",
        "    <td class=\"devsite-click-to-copy\"><a href=\"https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-2.13.0.zip\">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-2.13.0.zip</a></td>\n",
        "  </tr>\n",
        "  <tr>\n",
        "    <td>Windows GPU only</td>\n",
        "    <td class=\"devsite-click-to-copy\"><a href=\"https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-windows-x86_64-2.10.0.zip\">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-windows-x86_64-2.10.0.zip</a></td>\n",
        "  </tr>\n",
        "</table><br>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "b4kWu6k0FaT9"
      },
      "source": [
        "Extract the downloaded archive, which contains the header files to include in\n",
        "your C program and the shared libraries to link against.\n",
        "\n",
        "On Linux and macOS, you may want to extract to `/usr/local/lib`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "DrjVyjVJFcon"
      },
      "outputs": [],
      "source": [
        "%%bash\n",
        "FILENAME=libtensorflow-cpu-linux-x86_64-2.13.0.tar.gz\n",
        "wget -q --no-check-certificate https://storage.googleapis.com/tensorflow/libtensorflow/${FILENAME}\n",
        "sudo tar -C /usr/local -xzf ${FILENAME}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fcBJDdojJDyk"
      },
      "source": [
        "### Linker\n",
        "\n",
        "On Linux/macOS, if you extract the TensorFlow C library to a system directory,\n",
        "such as `/usr/local`, configure the linker with `ldconfig`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "h0STAG82JDZs"
      },
      "outputs": [],
      "source": [
        "%%bash\n",
        "sudo ldconfig /usr/local/lib"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ix4HdnNGH6aF"
      },
      "source": [
        "If you extract the TensorFlow C library to a non-system directory, such as\n",
        "`~/mydir`, then configure the linker environmental variables:"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "E6E99eJzIJQs"
      },
      "source": [
        "<div class=\"ds-selector-tabs\">\n",
        "<section>\n",
        "<h3>Linux</h3>\n",
        "<pre class=\"prettyprint lang-bsh\">\n",
        "export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib\n",
        "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/mydir/lib\n",
        "</pre>\n",
        "</section>\n",
        "<section>\n",
        "<h3>macOS</h3>\n",
        "<pre class=\"prettyprint lang-bsh\">\n",
        "export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib\n",
        "export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:~/mydir/lib\n",
        "</pre>\n",
        "</section>\n",
        "</div><!--/ds-selector-tabs-->"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qYVWjxqaJVPs"
      },
      "source": [
        "## Build"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UoMUuMJrJXp8"
      },
      "source": [
        "### Example program\n",
        "\n",
        "With the TensorFlow C library installed, create an example program with the\n",
        "following source code (`hello_tf.c`):"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "b5851f1b"
      },
      "outputs": [],
      "source": [
        "%%writefile hello_tf.c\n",
        "#include <stdio.h>\n",
        "#include <tensorflow/c/c_api.h>\n",
        "\n",
        "int main() {\n",
        "  printf(\"Hello from TensorFlow C library version %s\\n\", TF_Version());\n",
        "  return 0;\n",
        "}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "H1GFidbrIWzU"
      },
      "source": [
        "### Compile\n",
        "\n",
        "Compile the example program to create an executable, then run:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Jph67SAjIX0M"
      },
      "outputs": [],
      "source": [
        "%%bash\n",
        "gcc hello_tf.c -ltensorflow -o hello_tf\n",
        "\n",
        "./hello_tf"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0qtHXROoJwoz"
      },
      "source": [
        "Success: The TensorFlow C library is configured.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YbqbjqOSJ0IL"
      },
      "source": [
        "If the program doesn't build, make sure that `gcc` can access the TensorFlow C\n",
        "library. If extracted to `/usr/local`, explicitly pass the library location to\n",
        "the compiler:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "CdPmM35VJ_77"
      },
      "outputs": [],
      "source": [
        "%%bash\n",
        "gcc -I/usr/local/include -L/usr/local/lib hello_tf.c -ltensorflow -o hello_tf\n",
        "\n",
        "./hello_tf"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ea5fd208"
      },
      "source": [
        "## Build from source\n",
        "\n",
        "TensorFlow is open source. Read\n",
        "[the instructions](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/lib_package/README.md){:.external}\n",
        "to build TensorFlow's C library from source code."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "lang_c.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
